package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.ILocalQueryScenePersonInfoService;
import com.iwhalecloud.bss.kite.cucc.client.api.batch.IBatchConfigService;
import com.iwhalecloud.bss.kite.cucc.client.dto.batch.KiteBatchConfig;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.BusiImportInfoVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lyx
 * @version V1.0
 * @Description:
 * @date 2021/5/31
 * @Package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module
 */
@Service
public class BusiImportModuleHandler extends AbstractModuleHandler {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private ILocalQueryScenePersonInfoService queryScenePersonService;

    @Autowired
    private IBatchConfigService localBatchAcceptService;

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        SceneNewOfferSeneVO sceneAccountInfoVo = (SceneNewOfferSeneVO) context.getSceneVo();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        BusiImportInfoVO busiImportInfoVO = new BusiImportInfoVO();
        KiteSceneInst sceneInst = this.querySceneInstService.getSceneInst(sceneInstId);
        String rootInstId = sceneInst.getRootInstId();
        KiteOfferInst offerInst = querySceneInstService.getOfferInst(sceneInst.getCustId(), rootInstId);
        busiImportInfoVO.setOfferInstId(offerInst.getOfferInstId());
        busiImportInfoVO.setOfferName(offerInst.getOfferName());

        String spu = offerInst.getAttrValue("saleGoodsCode");
//        String spu = querySceneInstService.getExtParameterValue(sceneInstId, "spu");
        if (KiteStringUtils.isEmpty(spu)) {
            spu = querySceneInstService.getExtParameterValue(sceneInstId, "saleGoodsCode");
        }
        busiImportInfoVO.setOfferId(spu);
//        String applyRegionCode = sceneInst.getProvinceNbr();
//        KiteBatchConfig kiteBatchConfig = localBatchAcceptService
//            .queryConfigByOfferId(applyRegionCode,offerInst.getAttrValue("cateId"), spu);
//        if (Objects.nonNull(kiteBatchConfig)) {
//            busiImportInfoVO.setDownloadUrl(kiteBatchConfig.getTmplFileUrl());
//        }

        setImportAttr(sceneInstId, context.getAcceptRequest().getServiceOfferId(), busiImportInfoVO);
        setTitleAttr(sceneInstId, busiImportInfoVO);
        sceneAccountInfoVo.setBusiImportInfoVO(busiImportInfoVO);
        return Collections.singletonList(busiImportInfoVO);
    }

    private void setImportAttr(String sceneInstId, String serviceOfferId, BusiImportInfoVO busiImportInfoVO) {
        List<DcPublic> importConfigs = DcPublicCache.getByPkey("2021053101", serviceOfferId);
        List<String> importName = new ArrayList<>();
        if (KiteListUtils.isNotEmpty(importConfigs)) {
            importName = importConfigs.stream().map(DcPublic::getCodea).collect(Collectors.toList());
        }
        busiImportInfoVO.setImportButton(importName);
    }

    private void setTitleAttr(String sceneInstId, BusiImportInfoVO busiImportInfoVO) {
        List<InstAttrDTO> attrConfigs = queryScenePersonService
            .listKiteConfigAttrs(sceneInstId, LocalKeyConsts.BATCH_IMPORT_OBJECT_TYPE).getResultObject().stream()
            .filter(instAttrDTO -> LocalKeyConsts.KITE_ATTR_CONFIG_SHOW_TYPE_D.equals(instAttrDTO.getShowType()))
            .collect(Collectors.toList());
        List<CommonAttrVO> titleCommonAttrs = KiteBeanUtils.copyPropsForList(attrConfigs, CommonAttrVO.class);
        busiImportInfoVO.setTitleAttr(titleCommonAttrs);
    }

}
